import { createRouter, createWebHistory } from 'vue-router'

import routes from './routerConfig'

//导入进度条js库
import nprogress from 'nprogress'
import "nprogress/nprogress.css"

import {useLoginStore} from '@/stores/login'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
  //播放进度条动画
  nprogress.start();
  //动态修改网页标题
  document.title = to.meta.title +'';

  //从pinia中获取token
  var loginStore = useLoginStore()
  var token = loginStore.loginData.token;
  
  //路由鉴权
  if( token ){
    next();
  }else{
    if( to.path == '/login' ){
      next();
    }else{
      next('/login');
    }
  }
})
router.afterEach((to,from)=>{
  //停止播放进度条动画
  nprogress.done();
})

export default router
